/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.vale.services;

import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import org.apache.log4j.Logger;

/**
 *
 * @author Gabriel Bran gbran.u@gmail.com
 */
@Loggable
@Interceptor
public class LoggerInterceptor {

    @Inject
    private transient Logger logger;

    @AroundInvoke
    public Object logMethod(InvocationContext ic) throws Exception {
        try {
            return ic.proceed();
        } catch (Exception e) {
            logger.error(">>> " + ic.getTarget().getClass().getName() + "-" + ic.getMethod().getName(), e);
            throw new Exception(e.getMessage());
        } finally {
            logger.info("<<< " + ic.getTarget().getClass().getName() + "-" + ic.getMethod().getName());
        }
    }
}
